
/*
 * makebookmark.js
 *
 * Make Bookmark Firefox extension
 *
 * (c) 2010, Bipin Patwardhan
 */

var objMakeBookmark = {
	ConsoleService : Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService),
	
	Init: function()
	{
		var c = Components
		var nsSS = c.interfaces.nsISupportsString

		document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", objMakeBookmark.Popup, true);

		var oPref = c.classes["@mozilla.org/preferences-service;1"].getService(c.interfaces.nsIPrefService).getBranch("extensions.")
		var ss = c.classes[ "@mozilla.org/supports-string;1" ].createInstance(nsSS)
		ss.data = document.getElementById("makebookmark-context").getAttribute('statustext')
		oPref.setComplexValue("{103B2EA2-F063-4273-958D-6B46E5B6F98C}.description", nsSS, ss)
	},
	
	Log: function(aMessage)
	{
		this.ConsoleService.logStringMessage('MakeBookmark: ' + aMessage);
	},

	Popup: function()
	{
		document.getElementById("makebookmark-context").hidden = (!gContextMenu.onLink)
	},
	
	PlaceOnClipboard: function(aText)
	{
		var clipboard = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
		clipboard.copyString(aText);
		return true;
	},

	RemoveRedirection: function(origLink)
	{
		var k,x,t,i,j,p;
		
		t = origLink.replace(/[%]3A/ig,':').replace(/[%]2f/ig,'/');
		i = t.lastIndexOf('http');
		if ( i > 0 ) {
			t = t.substring(i);
			
			j = t.indexOf('&');
			if ( j > 0 ) {
				t = t.substring(0,j);
			}
		}
		
		return t;
	},

	getCurrentURL: function()
	{
		return window.content.location.href;
	},

	getSelectedText: function()
	{
		var txt = content.getSelection();
		if ( txt == '' ) {
			return '[text-not-selected]';
		}
		
		return txt;
	},
	
	/*
	 * copy name of url
	 */
	CopyLinkName: function()
	{
		var aText;
		aText = gContextMenu.linkText();
		objMakeBookmark.PlaceOnClipboard(aText);
	},

	/*
	 * copy name of url and link of url to for 'name | link'
	 */
	CopyLinkNameAndLinkAddress: function()
	{
		var aText;
		var aURL;

		aText = gContextMenu.linkText();
		aURL = gContextMenu.linkURL;
		objMakeBookmark.PlaceOnClipboard(aText + ' | ' + aURL);
	},

	/*
	 * copy link of url and name of url to for 'link | name'
	 */
	CopyLinkAddressAndLinkName: function()
	{
		var aURL;
		var aText;
		
		aURL = gContextMenu.linkURL;
		aText = gContextMenu.linkText();
		objMakeBookmark.PlaceOnClipboard(aURL + ' | ' + aText);
	},
	
	/*
	 * copy and link of url after removing redirection
	 */
	CopyLinkAddressRemoveRedirect: function()
	{
		var aURL;
		aURL = objMakeBookmark.RemoveRedirection(gContextMenu.linkURL);
		objMakeBookmark.PlaceOnClipboard(aURL);
	},

	/*
	 * copy name of url and link of url to for 'name | link' after removing redirection
	 */
	CopyLinkNameAndLinkAddressRemoveRedirect: function()
	{
		var aText;
		var aURL;
		
		aText = gContextMenu.linkText();
		aURL = objMakeBookmark.RemoveRedirection(gContextMenu.linkURL);
		objMakeBookmark.PlaceOnClipboard(aText + ' | ' + aURL);
	},

	/*
	 * copy link of url and name of url to for 'link | name' after removing redirection
	 */
	CopyLinkAddressAndLinkNameRemoveRedirect: function()
	{
		var aURL;
		var aText;
		
		aURL = objMakeBookmark.RemoveRedirection(gContextMenu.linkURL);
		aText = gContextMenu.linkText();
		objMakeBookmark.PlaceOnClipboard(aURL + ' | ' + aText);
	},
	
	/*
	 * copy highlighted text and link of url to for 'text | link'
	 */
	CopySelectedTextAndLinkAddress: function()
	{
		var aText;
		var aURL;

		aText = objMakeBookmark.getSelectedText();
		aURL = gContextMenu.linkURL;
		objMakeBookmark.PlaceOnClipboard(aText + ' | ' + aURL);
	},

	/*
	 * copy link of url and highlighted text to for 'link | text'
	 */
	CopyLinkAddressAndSelectedText: function()
	{
		var aURL;
		var aText;
		
		aURL = gContextMenu.linkURL;
		aText = objMakeBookmark.getSelectedText();
		objMakeBookmark.PlaceOnClipboard(aURL + ' | ' + aText);
	},
	
	/*
	 * copy highlighted text and link of url to for 'text | link' after removing redirection
	 */
	CopySelectedTextAndLinkAddressRemoveRedirect: function()
	{
		var aText;
		var aURL;
		
		aText = objMakeBookmark.getSelectedText();
		aURL = objMakeBookmark.RemoveRedirection(gContextMenu.linkURL);
		objMakeBookmark.PlaceOnClipboard(aText + ' | ' + aURL);
	},

	/*
	 * copy link of url and highlighted text to for 'link | text' after removing redirection
	 */
	CopyLinkAddressAndSelectedTextRemoveRedirect: function()
	{
		var aURL;
		var aText;
		
		aURL = objMakeBookmark.RemoveRedirection(gContextMenu.linkURL);
		aText = objMakeBookmark.getSelectedText();
		objMakeBookmark.PlaceOnClipboard(aURL + ' | ' + aText);
	},
	
	/*
	 * copy hightlighted text and current browser url to for 'text | link'
	 */
	CopySelectedTextAndCurrentURL: function()
	{
		var aText;
		var aURL;
		
		aText = objMakeBookmark.getSelectedText();
		aURL = objMakeBookmark.getCurrentURL();
		objMakeBookmark.PlaceOnClipboard(aText + ' | ' + aURL);
	},

	/*
	 * copy current browser url and highlighted txt to for 'link | text'
	 */
	CopyCurrentURLAndSelectedText: function()
	{
		var aText;
		var aURL;
		
		aText = objMakeBookmark.getSelectedText();
		aURL = objMakeBookmark.getCurrentURL();
		objMakeBookmark.PlaceOnClipboard(aURL + ' | ' + aText);
	}
};

window.addEventListener("load", function() { objMakeBookmark.Init(); }, false)

/*
 * end
 */
